class SessionController < ApplicationController

  def new
    session[:return_to] = request.referer
  end

  def create
    self.current_user = User.authenticate(params[:login], params[:password])
    if logged_in?
      if params[:remember_me] == "1"
        current_user.remember_me unless current_user.remember_token?
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
      end
      redirect_back_or_default('/')
      flash[:update] = "Wahoo! Now you're logged in, which means you can make comments!"
    else
      flash[:error] = "The user/password combination you entered was incorrect. Try again."
      render :action => 'new'
    end
  end

  def destroy
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:update] = "You have been logged out."
    redirect_back_or_default('/')
  end
end
